﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;



namespace ForumDB
{
	public class Program
	{
		private VER3 db_forum; 
		//private UserFactory factory;

		public Program()
		{
			db_forum = new VER3(@"C:\Users\bbyytt\Documents\Visual Studio 2010\Projects\ForumDB\ForumDB\VER3.mdf");
			//factory = new UserFactory(db_forum);
		}


		/*
		public Query getPage(Location p, int size, int pageNumber)
		{
			return factory.getPageMethod(p.type);
		}
		*/

//********************************************************************************
		//Page getPage(Position p, int size, int pageNumber);
//********************************************************************************
		//what is int size, int pageNumber?
		//Location is (name of table + id in table) -- actully its for the server but i need only id

		public Query getPost(int concretePost)
		{
			var postQuery =
			from post in db_forum.Posts
			where post.PostID == concretePost
			select post;
			return new PostQuery(postQuery);
		}

		public Query getThread(int concreteThread)
		{
			var threadQuery =
			from thread in db_forum.Threads
			where thread.ThreadID == concreteThread
			select thread;
			return new ThreadQuery(threadQuery);
		}

		public Query getSubForum(int concreteSubForum)
		{
			var subForumQuery =
			from subForum in db_forum.SubForums
			where subForum.SubForumID == concreteSubForum
			select subForum;
			return new SubForumQuery(subForumQuery);
		}




		public Query getAllSubForum(int size, int pageNumber)
		{
			var subForumQuery =
			from subForum in db_forum.SubForums
			select subForum;
			return new SubForumQuery(subForumQuery.Skip(pageNumber*size).Take(size));
		}
		
		public Query getAllThread(int _subForumID, int size, int pageNumber)
		{
			var threadQuery =
			from thread in db_forum.Threads
			where thread._SubForumID == _subForumID
			select thread;
			return new ThreadQuery(threadQuery);
		}

		public Query getAllPost(int _threadID, int size, int pageNumber)
		{
			var postQuery =
			from post in db_forum.Posts
			where post._ThreadID == _threadID
			select post;
			return new PostQuery(postQuery);
		}

//***************************************************************************************************************
//***************************************************************************************************************

		public void addPost(int _threadLocation, string title, string body, string clientID, DateTime time)
		{	
			Post newPost = new Post();
			//newPost.PostID - NO NEED - AUTO INCREMENT;
			newPost._ThreadID = _threadLocation;  
			newPost.Title = title;
			newPost.Body = body;
			newPost._ClientID = clientID; // STRING OR INT?
			newPost.Time = time;
			newPost.Description = "NOT IN USE Post";

			// Add the Post to the Posts table.
			db_forum.Posts.InsertOnSubmit(newPost);
			Console.WriteLine("\nInserting Post");
			db_forum.SubmitChanges();
		}

		public void addThread(int _subForumLocation, string title, string body, string clientID, DateTime time)
		{
			Thread newThread = new Thread();
			//newPost.PostID - NO NEED - AUTO INCREMENT;
			newThread._SubForumID = _subForumLocation;
			newThread.Title = title;
			newThread.Body = body;
			newThread._ClientID = clientID; // STRING OR INT?
			newThread.DateTime = time;
			newThread.Description = "NOT IN USE Thread";

			// Add the Post to the Posts table.
			db_forum.Threads.InsertOnSubmit(newThread);
			Console.WriteLine("\nInserting Thread");
			db_forum.SubmitChanges();
		}

		public void addSubForum(string title, string moderator, DateTime time)
		{
			SubForum newSubForum = new SubForum();
			newSubForum.Title = title;
			newSubForum.Moderator = moderator;
			newSubForum.DateTime = time;
			newSubForum.Description = "NOT IN USE SubForum";

			// Add the Post to the Posts table.
			db_forum.SubForums.InsertOnSubmit(newSubForum);
			Console.WriteLine("\nInserting SubForum");
			db_forum.SubmitChanges();
		}

//***************************************************************************************************************
//***************************************************************************************************************


		public void removeThread(Location p) 
		{
			var deletePosts =
			from post in db_forum.Posts
			where post._ThreadID == p.id
			select post;
			db_forum.Posts.DeleteAllOnSubmit(deletePosts);

			var existingThread =
			(from thread in db_forum.Threads
			where thread.ThreadID == 3//p.id
			select thread)
			.First();
			db_forum.Threads.DeleteOnSubmit(existingThread);

			db_forum.SubmitChanges();
		}





	

	}


}





// Create the new Customer object.
/*
var checks =
from check in db_forum.Checks
where check.ID == 12
select check;

Console.WriteLine(checks.Count());

Check ben = new Check();
ben.ID = 99;
ben.DESCRIPTION = "ddddd";
db_forum.Checks.InsertOnSubmit(ben);
Console.WriteLine("\nCustomers matching CA before insert");
db_forum.SubmitChanges();
*/